/*
 * Copyright (C) 2023 HiHope Open Source Organization .
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <unistd.h>

#include "ohos_init.h"
#include "cmsis_os2.h"

#include "hi_gpio.h"
#include "hi_io.h"
#include "hi_adc.h"
#include "hi_errno.h"

#include "iot_gpio.h"



#define STACK_SIZE   (1024)
#define DELAY_US     (100000)

#define     IOT_PWM_PORT_PWM0       0
#define     PWM_DUTY_50            (50)
#define     LIGHT_NUM               (3)
#define     PWM_FREQ_4K            (4000)
#define     IOT_GPIO_IDX_10    10  // Red
#define     IOT_GPIO_IDX_11    11  // Green
#define     IOT_GPIO_IDX_12    12  // Yellow
#define     IOT_GPIO_IDX_9     9   // Beep

#define     DELAY_100MS        (100 * 1000)
#define     DELAY_200MS        (200 * 1000)

static      unsigned int pins[LIGHT_NUM] = {IOT_GPIO_IDX_10, IOT_GPIO_IDX_12, IOT_GPIO_IDX_11};



static void clikeBTU(int index){

            //灯
            IoTGpioSetOutputVal(pins[index], IOT_GPIO_VALUE1);
            //响
            IoTPwmStart(IOT_PWM_PORT_PWM0, PWM_DUTY_50, PWM_FREQ_4K);
            usleep(DELAY_200MS);
            //停
            IoTPwmStop(IOT_PWM_PORT_PWM0);
            IoTGpioSetOutputVal(pins[index], IOT_GPIO_VALUE0);
            

}



static void AdcGpioTask(void)
{
    hi_u16 value;

while (1) {
                
        if (hi_adc_read(HI_ADC_CHANNEL_2, &value,
            HI_ADC_EQU_MODEL_4, HI_ADC_CUR_BAIS_DEFAULT, 0) != HI_ERR_SUCCESS) {

            printf("ADC read error!\n");
                

        } else if (100<value && value<130) {  

            //电平
            printf("ADC_VALUE = %u\n", (unsigned int)value);
            clikeBTU(0);

        } else if (300<value && value<400) { 


            printf("ADC_VALUE = %u\n", (unsigned int)value);
            clikeBTU(1);
            
        } else if (560<value && value<650) {  

            printf("ADC_VALUE = %u\n", (unsigned int)value);
            clikeBTU(2);
            
            
        } else {  



        }

    }
}




static void initALL(void){
	
	//初始化 	
    hi_gpio_init();

	//按键 
    
    hi_io_set_func(HI_GPIO_IDX_5, 0);
    hi_gpio_set_dir(HI_GPIO_IDX_5, HI_GPIO_DIR_IN);
	
	//灯 

	IoTGpioInit(IOT_GPIO_IDX_10);
    IoTGpioSetDir(IOT_GPIO_IDX_10, IOT_GPIO_DIR_OUT);

    IoTGpioInit(IOT_GPIO_IDX_11);
    IoTGpioSetDir(IOT_GPIO_IDX_11, IOT_GPIO_DIR_OUT);

    IoTGpioInit(IOT_GPIO_IDX_12);
    IoTGpioSetDir(IOT_GPIO_IDX_12, IOT_GPIO_DIR_OUT);

	//蜂鸣器 
	
	IoTGpioInit(IOT_GPIO_IDX_9);
    hi_io_set_func(IOT_GPIO_IDX_9, HI_IO_FUNC_GPIO_9_PWM0_OUT);
    IoTGpioSetDir(IOT_GPIO_IDX_9, IOT_GPIO_DIR_OUT);
    IoTPwmInit(IOT_PWM_PORT_PWM0);

	
	

	
}






static void AdcGpioEntry(void)
{
    printf("ADC Test!\n");
    osThreadAttr_t attr;

    initALL();

    attr.name = "AdcGpioTask";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = STACK_SIZE;
    attr.priority = osPriorityNormal;

    if (osThreadNew(AdcGpioTask, NULL, &attr) == NULL) {
        printf("[LedExample] Falied to create LedTask!\n");
    }
}
SYS_RUN(AdcGpioEntry);